package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blog.dao.mapper.TagMapper;
import com.example.blog.dao.pojo.Tag;
import com.example.blog.service.LoginService;
import com.example.blog.service.TagService;
import com.example.blog.vo.Result;
import com.example.blog.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    public  TagVo copy(Tag tag){
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(tag,tagVo);
        return tagVo;
    }
    public List<TagVo> copyList(List<Tag> tagList){
        List<TagVo> tagVoList = new ArrayList<>();
        for (Tag tag : tagList) {
            tagVoList.add(copy(tag));
        }
        return tagVoList;
    }


    @Override
    public Result host(int limit) {
        /**
         * 1.标签所游泳的文章数量最多就是最热标签
         * 2.查询 根据tag的id分组计数 从大到小排列 取前limit个
         */
        List<Long> tagIds = tagMapper.finHostTageIds(limit);
        /**
         * 需求需要id和名称
         */
        List<Tag> tagList = tagMapper.findTagsByTagIds(tagIds);
        return Result.success(tagList);
    }

    @Override
    public Result findTagsNew() {
        List<Tag> tagsNew = tagMapper.findTagsNew();
        return Result.success(tagsNew);
    }

    @Override
    public Result findAll() {
        List<Tag> tags = this.tagMapper.selectList(new LambdaQueryWrapper<>());
        return Result.success(copyList(tags));
    }

    @Override
    public Result findTagsByCategoryID(Long categoryId) {
        LambdaQueryWrapper<Tag> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Tag::getCategoryId,categoryId);
        List<Tag> tags = this.tagMapper.selectList(lambdaQueryWrapper);
        return Result.success(tags);
    }

    @Override
    public Result findTagDetailById(Long tid) {
        Tag tag = tagMapper.selectById(tid);

        return Result.success(copy(tag));
    }
}
